package com.example.a79273.shandianban;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.application.MyApplication;
import com.example.a79273.shandianban.fragment.YouRen;
import com.squareup.picasso.Picasso;

public class SettingActivity extends AppCompatActivity implements View
        .OnClickListener {
    private RelativeLayout rl_setting_name, rl_setting_sex, rl_setting_phone,
            rl_setting_us;
    private ImageView iv_setting_icon, iv_setting_back;
    private TextView tv_setting_save, tv_setting_name, tv_setting_sex_real,
            tv_setting_phone, tv_setting_quit, tv_pop_icon_camera,
            tv_pop_icon_album,tv_pop_icon_cancel;
    private View popViewIcon, rootView;
    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;
    private PopupWindow popupWindow;
    private MyApplication myApplication;
    private Bitmap bitmap;
    private String name2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        rl_setting_name.setOnClickListener(this);
        rl_setting_sex.setOnClickListener(this);
        rl_setting_us.setOnClickListener(this);
        rl_setting_phone.setOnClickListener(this);
        iv_setting_back.setOnClickListener(this);
        iv_setting_icon.setOnClickListener(this);
        tv_setting_save.setOnClickListener(this);
        tv_setting_quit.setOnClickListener(this);
        tv_pop_icon_album.setOnClickListener(this);
        tv_pop_icon_camera.setOnClickListener(this);
        tv_pop_icon_cancel.setOnClickListener(this);
    }

    private void setData() {
        popupWindow = new PopupWindow(popViewIcon, ViewGroup.LayoutParams
                .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    private void initData() {
        myApplication = MyApplication.getInstance();

        builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("您确定要退出当前账号").setNegativeButton("取消", new
                DialogInterface.OnClickListener() {


            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myApplication.setLogin(false);
                Intent intent = new Intent(SettingActivity.this, MainActivity
                        .class);
                startActivity(intent);
                finish();
            }
        });
        alertDialog = builder.create();

    }

    private void initView() {
        rl_setting_name = (RelativeLayout) findViewById(R.id.rl_setting_name);
        rl_setting_phone = (RelativeLayout) findViewById(R.id.rl_setting_phone);
        rl_setting_sex = (RelativeLayout) findViewById(R.id.rl_setting_sex);
        rl_setting_us = (RelativeLayout) findViewById(R.id.rl_setting_us);
        iv_setting_icon = (ImageView) findViewById(R.id.iv_setting_icon);
        iv_setting_back = (ImageView) findViewById(R.id.iv_setting_back);
        tv_setting_save = (TextView) findViewById(R.id.tv_setting_save);
        tv_setting_name = (TextView) findViewById(R.id.tv_setting_name);
        tv_setting_sex_real = (TextView) findViewById(R.id.tv_setting_sex_real);
        tv_setting_phone = (TextView) findViewById(R.id.tv_setting_phone);
        tv_setting_quit = (TextView) findViewById(R.id.tv_setting_quit);
        popViewIcon = LayoutInflater.from(this).inflate(R.layout.pop_icon, null);
        rootView = LayoutInflater.from(this).inflate(R.layout.activity_setting,
                null);

        tv_pop_icon_camera= (TextView) popViewIcon.findViewById(R.id.tv_pop_icon_camera);
        tv_pop_icon_cancel= (TextView) popViewIcon.findViewById(R.id.tv_pop_icon_cancel);
        tv_pop_icon_album= (TextView) popViewIcon.findViewById(R.id.tv_pop_icon_album);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.rl_setting_name:
                Intent intent = new Intent(this, NameSetActivity.class);
                String name1 = tv_setting_name.getText().toString();
                intent.putExtra("name1", name1);
                startActivityForResult(intent, 1);
                break;
            case R.id.rl_setting_phone:

                break;
            case R.id.rl_setting_sex:

                break;
            case R.id.rl_setting_us:

                break;
            case R.id.iv_setting_back:
                finish();
                break;
            case R.id.iv_setting_icon:
                popupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.tv_setting_save:
                Toast.makeText(SettingActivity.this, "保存修改", Toast.LENGTH_SHORT).show();
                tv_setting_name.setText(name2);
                myApplication.setIcon(bitmap);
                break;
            case R.id.tv_setting_quit:
                alertDialog.show();
                break;
            case R.id.tv_pop_icon_album:
            popupWindow.dismiss();
            Intent intentAlbum=new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(intentAlbum,20);
                break;
             case R.id.tv_pop_icon_cancel:
                 popupWindow.dismiss();
                 break;
             case R.id.tv_pop_icon_camera:
                 popupWindow.dismiss();
                 Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用android自带的照相机
                 startActivityForResult(intentCamera, 10);
                 break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent
            data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            name2 = data.getStringExtra("name2");
            tv_setting_name.setText(name2);

        }
        if(requestCode==10&&resultCode== Activity.RESULT_OK&&resultCode!=Activity.RESULT_CANCELED){
            Bundle bundle = data.getExtras();
            bitmap = (Bitmap)bundle.get("data");

            iv_setting_icon.setImageBitmap(bitmap);

        }
        if(requestCode==20&&resultCode!=Activity.RESULT_CANCELED){
            Uri uri = data.getData();
            Cursor cursor = this.getContentResolver().query(uri, null,
                    null, null, null);
            cursor.moveToFirst();
            String imgNo = cursor.getString(0); // 图片编号
            String imgPath = cursor.getString(1); // 图片文件路径
            String imgSize = cursor.getString(2); // 图片大小
            String imgName = cursor.getString(3); // 图片文件名
            cursor.close();
            bitmap =  BitmapFactory.decodeFile(imgPath);
            iv_setting_icon.setImageBitmap(bitmap);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        popupWindow.dismiss();
        return super.onTouchEvent(event);
    }
}
